package com.moli.iov.gateway.util;

import com.moli.iov.protocol.enums.CommandTag;
import com.moli.iov.protocol.enums.ResponseTag;
import com.moli.iov.protocol.util.BccUtils;
import com.moli.iov.util.ByteArrayUtil;

/**
 * @author fudh
 * @ClassNmme ResponseBytesUtils
 * @date 2018/11/29 18:04
 * @Description: 拼接回包数据工具类
 */
public class ResponseBytesUtils {


    static final String CONSTANT_VIN = "*****************";

    /**
     * 根据请求报文 组装返回报文 此方法特殊组织方式替换启动的VIN
     * @param reqBytes
     * @param vin      vin号
     * @param flag     true,应答标志是0x01;false,0x02
     */
    public static byte[] joinLoginRespBytes(byte[] reqBytes, String vin, boolean flag) {
        byte[] retBytes = null;
        if (reqBytes.length > 30) {
            byte[] newBytes = new byte[30];
            byte[] bccBytes = new byte[reqBytes.length - 1];
            System.arraycopy(reqBytes, 0, bccBytes, 0, reqBytes.length - 1);
            // 组装报文头
            newBytes[0] = reqBytes[0];// 0x23
            newBytes[1] = reqBytes[1];// 0x23
            newBytes[2] = reqBytes[2];// 命令标示
            if (BccUtils.checkBccCode(reqBytes)) {// 命令回应标示
                if (!CONSTANT_VIN.equals(vin) && flag) {
                    newBytes[3] = 0x01;
                } else {
                    // vin为*****************则说明未找到vin与ICCID对应关系,则返回错误命令标识
                    newBytes[3] = 0x02;
                }
            } else {
                newBytes[3] = 0x02;
            }
            byte[] newVinBytes = ByteArrayUtil.strToAscii(vin);
            // 拷贝VIN
            System.arraycopy(newVinBytes, 0, newBytes, 4, 17);
            newBytes[21] = reqBytes[21];
            newBytes[22] = 0x00;
            newBytes[23] = 0x06;
            // 拷贝采集时间
            System.arraycopy(reqBytes, 24, newBytes, 24, 6);
            retBytes = BccUtils.bccBodyEncode(newBytes);
        }
        return retBytes;
    }

    /**
     * 根据请求报文 组装返回报文
     *
     * @param reqBytes
     * @param flag     true,封装01的成功的回包；false，返回02失败的回包
     */
    public static byte[] joinRespBytes(byte[] reqBytes, boolean flag) {
        return joinRespBytes(reqBytes, null, flag);
    }

    /**
     * @param reqBytes    报文
     * @param commandByte 命令标识
     * @param flag        true,封装01的成功的回包；false，返回02失败的回包
     * @return
     */
    public static byte[] joinRespBytes(byte[] reqBytes, Byte commandByte, boolean flag) {

        byte[] retBytes = null;
        CommandTag cTag = CommandTag.valOf(reqBytes[2]);
        byte[] newBytes = null;
        if (cTag == CommandTag.HEARTBEAT) {
            newBytes = new byte[reqBytes.length - 1];
            System.arraycopy(reqBytes, 0, newBytes, 0, reqBytes.length - 1);
            newBytes[3] = ResponseTag.SUCCESS.getV();
        } else if (cTag == CommandTag.VEHICLE_REGISTER) {//注册
            newBytes = new byte[36];
            System.arraycopy(reqBytes, 0, newBytes, 0, 32);
            newBytes[2] = (byte) 0xDC;//注册应答包和回包命令标识不一致
            newBytes[3] = (byte) 0x01;//应答标志，默认成功为0
            newBytes[22] = 0x00;//数据单元长度
            newBytes[23] = 0x0C;//数据单元长度
        } else {
            newBytes = new byte[reqBytes.length - 1];
            byte[] bccBytes = new byte[reqBytes.length - 1];
            System.arraycopy(reqBytes, 0, bccBytes, 0, reqBytes.length - 1);
            // 组装报文头
            newBytes[0] = reqBytes[0];// 0x23
            newBytes[1] = reqBytes[1];// 0x23
            newBytes[2] = (commandByte == null ? reqBytes[2] : commandByte);// 命令标示
            // 拷贝VIN
            System.arraycopy(reqBytes, 4, newBytes, 4, reqBytes.length - 5);
        }
        if (flag) {// 命令回应标示
            newBytes[3] = 0x01;
        } else {
            newBytes[3] = 0x02;
        }
        retBytes = BccUtils.bccBodyEncode(newBytes);
        return retBytes;
    }


//    public static void main(String[] args) {
//        String str = "232301FE363631443030303030303030303030303201002A12060C0E0A0700133839383630324439393831373330303034393532010C313138323133333035353031E2";
//        byte[] bytes = joinRespBytes_resetTime(ByteArrayUtil.hexStringToBytes(str), new DateTimeTag().toBytes(), true);
//        System.out.println(ByteArrayUtil.toHexString(bytes));
//    }

    /**
     * 根据请求报文 组装返回报文 重新设置时间
     *
     * @param reqBytes
     * @param time
     * @return
     */
    public static byte[] joinRespBytes_resetTime(byte[] reqBytes, byte[] time, boolean flag) {

        byte[] retBytes = null;
        CommandTag cTag = CommandTag.valOf(reqBytes[2]);
        if (cTag == CommandTag.HEARTBEAT) {
            byte[] newBytes = new byte[reqBytes.length - 1];
            System.arraycopy(reqBytes, 0, newBytes, 0, reqBytes.length - 1);
            newBytes[3] = ResponseTag.SUCCESS.getV();
            retBytes = BccUtils.bccBodyEncode(newBytes);
        } else if (cTag == CommandTag.VEHICLE_REGISTER) {
            if (reqBytes.length > 24) {
                byte[] newBytes = new byte[35];
                byte[] bccBytes = new byte[reqBytes.length - 1];
                System.arraycopy(reqBytes, 0, bccBytes, 0, reqBytes.length - 1);
                // 组装报文头
                newBytes[0] = reqBytes[0];// 0x23
                newBytes[1] = reqBytes[1];// 0x23
                newBytes[2] = (byte) 0xDC;// 命令标示
                if (BccUtils.checkBccCode(reqBytes) && flag) {// 命令回应标示
                    newBytes[3] = 0x01;
                } else {
                    newBytes[3] = 0x02;
                }
                // 拷贝VIN
                System.arraycopy(reqBytes, 4, newBytes, 4, 17);
                newBytes[21] = reqBytes[21];
                newBytes[22] = 0x00;
                newBytes[23] = 0x0C;
                // 使用设置的时间
                System.arraycopy(time, 0, newBytes, 24, 6);
                retBytes = BccUtils.bccBodyEncode(newBytes);
            }
        } else {
            if (reqBytes.length > 24) {
                byte[] newBytes = new byte[30];
                byte[] bccBytes = new byte[reqBytes.length - 1];
                System.arraycopy(reqBytes, 0, bccBytes, 0, reqBytes.length - 1);
                // 组装报文头
                newBytes[0] = reqBytes[0];// 0x23
                newBytes[1] = reqBytes[1];// 0x23
                newBytes[2] = reqBytes[2];// 命令标示
                if (BccUtils.checkBccCode(reqBytes) && flag) {// 命令回应标示
                    newBytes[3] = 0x01;
                } else {
                    newBytes[3] = 0x02;
                }
                // 拷贝VIN
                System.arraycopy(reqBytes, 4, newBytes, 4, 17);
                newBytes[21] = reqBytes[21];
                newBytes[22] = 0x00;
                newBytes[23] = 0x06;
                // 使用设置的时间
                System.arraycopy(time, 0, newBytes, 24, 6);
                retBytes = BccUtils.bccBodyEncode(newBytes);
            }
        }
        return retBytes;
    }

}
